﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
using System.Text;
using System.Text.RegularExpressions;
using HZ.Supervise.IBLL;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class HospitalStatisticsMX : BasePage
    {
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();
        IBLL_User iu = BLLFactory.BLL_User();
        SuperviseQuery vs = new SuperviseQuery();
        DateTime startime;
        DateTime endtime;
        string where;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStratTime.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
                txtEndTime.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
                ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID; 
            }
        }

        /// <summary>
        /// 页面级绑定
        /// </summary>
        public void SetBind()
        {
            where = DAL_Helper.GetWhereCondition2<HZ.Supervise.Model.SuperviseQuery>(Queryby());
            string sql = string.Format(@"select Count(1) from View_PurchaseOrderDetail where OrderDetailState>0 {0}", where);
//            string sql = string.Format(@"select count(*) 
//                    from TB_PurchaseOrderDetail a inner join TB_PurchaseOrder b on a.OrderID=b.OrderID
//                    inner join TB_Goods c on a.ProcureCatalogID=c.GoodsID 
//                    inner join TB_Hospital d on b.HospitalID=d.HospitalID
//                    inner join TB_Company e on a.CompanyID_PS=e.CompanyID
//                    where OrderDetailState>0 {0}",where);
            int count = Convert.ToInt32(HZ.Data.DbHelperFactory.Create().ExecuteScalar(sql));
            if (count > 5000)
            {
                HZ.Web.WebUtil.Alert("查询数据量已达" + count + "条,建议缩小统计范围或后台统计!！", this);
            }
            else
            {
                string orderby = ""; //this.ddlFiled.SelectedValue + "  " + this.ddlDirection.SelectedValue;//排序字段与方式
                int totalrecord = 0;
                DataTable dt = i.GetHospitalStatisticsMX(orderby, where, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
                AspNetPager1.RecordCount = totalrecord;
                gvwInfo.PageSize = AspNetPager1.PageSize;
                gvwInfo.DataSource = dt;
                gvwInfo.DataBind();
                if (totalrecord == 0)
                {
                    this.btnExcel.Visible = false;
                }
                else
                {
                    this.btnExcel.Visible = true;
                }
                if (dt.Rows.Count > 0)
                {
                    DataTable dt2 = i.GetHospitalStatisticsCount(where);
                    int HospitalCount = Convert.ToInt32(dt2.Rows[0]["HospitalCount"]);
                    int CompanyCount = Convert.ToInt32(dt2.Rows[0]["CompanyCount"]);
                    int GoodsCount = Convert.ToInt32(dt2.Rows[0]["GoodsCount"]);
                    double Amount = Convert.ToDouble(dt2.Rows[0]["Amount"]);
                    this.lblCount.Text = startime.ToString("Y") + "至" + endtime.ToString("Y") + "期间,筛选区域共有" + HospitalCount + "家医疗机构和" + CompanyCount + "家配送企业发生采购行为,涉及" + GoodsCount + "个产品，总金额共" + Amount.ToString("0.00") + "元;";
                }
                else
                {
                    this.lblCount.Text = "";
                }
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            string orderby = "";//this.ddlFiled.SelectedValue + "  " + this.ddlDirection.SelectedValue;//排序字段与方式
            int totalrecord = 0;
            DataTable dt = i.GetHospitalStatisticsMX(orderby, where, 1, 0, out totalrecord);
            if (dt.Rows.Count > 0)
            {
                KeyOperationLogJG keyOperationLog = new KeyOperationLogJG();
                keyOperationLog.OperationLogSerialID = Guid.NewGuid().ToString();
                keyOperationLog.Operator = CurrentAccount.GetUser().UserID;
                keyOperationLog.OrgID = CurrentAccount.GetUser().Org.OrgID;
                keyOperationLog.OperateTime = DateTime.Now;
                keyOperationLog.OperateMessage = "导出了订单明细数据";
                ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
            }
            else
            {
                HZ.Web.WebUtil.Alert("暂无导出的数据！", this.Page);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
        }

        #region 查询条件
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns>where</returns>
        private SuperviseQuery Queryby()
        {
            vs.Append += "1=1";
            #region 查询条件_地区 
            //string id = CurrentAccount.GetUser().Org.Area.AreaID;
            //if (id == "510000")//省级监督机构
            //{
            //    ddlArea.DefaultAreaID = id;
            //    if (ddlArea.SelectedAreaID == "510000")
            //    {
            //        vs.Append += string.Format(" AND SUBSTRING( AreaID,1,2) ='{0}'" , ddlArea.SelectedAreaID.Substring(0, 2));
            //    }
            //    else if (ddlArea.SelectedAreaID.Substring(4, 2) == "00")
            //    {
            //        vs.Append += string.Format(" AND SUBSTRING( AreaID,1,4) ='{0}'" , ddlArea.SelectedAreaID.Substring(0, 4));
            //    }
            //    else
            //    {
            //        vs.Append += string.Format(" AND AreaID ='{0}'" , ddlArea.SelectedAreaID );
            //    }
            //}
            //else if (id.Substring(4, 2) == "00")  //市级
            //{
            //    ddlArea.DefaultAreaID = id;
            //    ddlArea.ddlCityEnabled = "false";
            //    if (ddlArea.SelectedAreaID.Substring(4, 2) == "00")
            //    {
            //        vs.Append += string.Format(" AND SUBSTRING( AreaID,1,4) ='{0}'" , ddlArea.SelectedAreaID.Substring(0, 4));
            //    }
            //    else
            //    {
            //        vs.Append += string.Format(" AND AreaID ='{0}'", ddlArea.SelectedAreaID);
            //    }
            //}
            //else
            //{
            //    ddlArea.DefaultAreaID = id;
            //    ddlArea.ddlCityEnabled = "false";
            //    ddlArea.ddlCountryEnabled = "false";
            //    vs.Append += string.Format(" AND AreaID ='{0}'", ddlArea.SelectedAreaID);
            //}
            #endregion
            vs.AreaID = ddlArea.SelectedAreaID;
            //查询订单提交时间段
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                startime = (DateTime)new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime;//开始时间
                vs.Append += string.Format(@" AND SubmiTime >='{0}'", startime);
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                endtime = (DateTime)new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime;//结束时间
                if (endtime >= Convert.ToDateTime(txtStratTime.Text.Trim()))
                {
                    endtime = endtime.AddMonths(1);
                    vs.Append += string.Format(" and SubmiTime < '{0}' ", endtime);
                }
                else
                {
                    HZ.Web.WebUtil.Alert("截止时间不能小于起始时间");
                }
            }
            //查询产品编号
            if (!string.IsNullOrEmpty(txtGoodsId.Text.Trim()))
            {
                vs.Append +=string.Format(@"AND GoodsId='{0}'", txtGoodsId.Text.Trim());
            }
            //查询目录名称
            if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
				vs.Append += string.Format(@"AND CHARINDEX('{0}',ProductName)>0", txtProductName.Text.Trim());
            }
            //查询产品注册证号
            if (!string.IsNullOrEmpty(txtRegCode.Text.Trim()))
            {
                vs.Append += string.Format(@" and CHARINDEX('{0}',RegCode)>0", txtRegCode.Text.Trim());
            }
            //查询规格型号
            if (!string.IsNullOrEmpty(txtOutlookc.Text.Trim()))
            {
                vs.Append +=string.Format(@"AND CHARINDEX('{0}',Outlookc)>0", txtOutlookc.Text.Trim());
            }
            //查询包装单位
            if (!string.IsNullOrEmpty(txtPackUnit.Text.Trim()))
            {
                vs.Append += string.Format(@" and CHARINDEX('{0}',PackUnit)>0", txtPackUnit.Text.Trim());
            }
            //查询医疗机构的名称
            if (!string.IsNullOrEmpty(txtHospitalName.Text.Trim()))
            {
                vs.Append +=string.Format(@" and CHARINDEX('{0}',HospitalName)>0", txtHospitalName.Text.Trim());
            }
            //查询生产企业的名称
            if (!string.IsNullOrEmpty(txtCompanyName.Text.Trim()))
            {
                vs.Append +=string.Format(@" and CHARINDEX('{0}',CompanyName_SC)>0", txtCompanyName.Text.Trim());
            }
            //查询申报企业的名称
            if (!string.IsNullOrEmpty(txtCompanyNameTB.Text.Trim()))
            {
                vs.Append += string.Format(@" and CHARINDEX('{0}',CompanyName_TB)>0", txtCompanyNameTB.Text.Trim());
            }
            //查询配送企业的名称
            if (!string.IsNullOrEmpty(txtCompanyName_PS.Text.Trim()))
            {
                vs.Append += string.Format(@" and CHARINDEX('{0}',CompanyName_PS)>0", txtCompanyName_PS.Text.Trim());
            }
            if (!string.IsNullOrEmpty(ddlOrderDetailState.SelectedValue))//订单状态
            {
                vs.Append += string.Format(" and OrderDetailState={0}", ddlOrderDetailState.SelectedValue);
            }
            User u = iu.GetUserByUserID(CurrentAccount.GetUser().UserID);
            if (u.IsJK == true)
            {
                vs.Append += string.Format(" and HospitalID like 'jk%'");
            }
            //排序

            return vs;
        }

        #endregion

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";
            }
        }












    }
}